import { defineStore } from "pinia";
import { decrypt } from "@/utils/index";
import {
  getsearch,
  getsupplier,
  getbuyer,
  getendering,
  getcompany,
  getproject,
  getcompany_name,
  getbxsubscription,
  getcompany_detail,
  getcompany_sum,
  getcompany_detail_zb,
  getcompany_detail_wzb,
  getcompany_contacts,
  getbrand,
  getforecast,
  getcontacts,
  getcompetitor,
  getcompany_contacts_detail,
  getcompany_detail_lx,
  getcompany_detail_zb_lx,
  detail_dl,
  tj_dl,
  lxr_dl
} from "@/Api/shoop/index";
import { userInfo } from '@/Api/api.js'
import { useCounterStore } from "../../pain/login/login";
import { ElMessage } from "element-plus";
const store = useCounterStore();
let tokens = localStorage.getItem("token");
let token
if (tokens) {
  userInfo().then(rex => {
    token = rex.data.data
  })
}

// 筛选
export const useSearchStore = defineStore("search", {
  state: () => ({
    count: [],
    address: [],
    flg: false,
  }),

  actions: {},
});

// 全部标讯
export const useRadio1Store = defineStore("radio1", {
  state: () => ({
    // 导出
    flag: true,
    derive: false,
    loading: false,
    xqshujuloading: false,
    level_id: "",
    title: {},
    // 全部标讯
    id: "1",
    total: "",
    dataLiss: [],
    count: {},
    totals: "",
    biaoxuntotals: "",
    page: {
      currentPage: 1,
      pageSize: 10,
    },
    arrange: {
      arrange: "1",
    }, //排序
    // 供应商
    gysloading: false,
    suppliers: {},
    suppliersLiss: [],
    summary: {}, // 中标次数
    // 采购商
    buyer: {},
    buyerLiss: {},
    // 品牌
    brands: {},
    brandsLiss: {},
    pinaiID: "3",
    xmdw: "", //医疗传过来的参数
    // 招标预测
    forecasts: {},
    forecastsLiss: {},
    // 拟建项目
    proposed: {},
    proposedLiss: {},
    // 招标代理机构
    agency: {},
    agencyLiss: {},
    // 公司
    corporation: {},
    corporLiss: {},

    // 详情
    xiangqing_id: "",
    company_name: "",
    solicitation: {},
    xiangqing: {},
    // 详情pages
    xiangqing_pages: {
      currentPage: 1,
      pageSize: 10,
    },
    // 招标人
    // zhaobiao_ren: "",
    zhaobiao_digital: {},
    zhaobiaoData: [],
    zhaobaioTiTle: "",
    zhaobiaoDatalog: false,
    //联系人
    lianxiName: [],
    zhaoblianxiren: "",
    zhongbiaolianxiren: "",
    // 中标人
    // zhongbiao_ren: "",
    zhongbiao_digital: {},
    zhongbiao_Name: [],
    zhongbiao_liss: [],
    zhongbaioTiTle: "",
    winatender: {},
    // 未中标
    weizhongbaioTj: {},
    weizhongbiaoLiss: [],
    weizhongbaioTiTle: "",
    // 联系人数
    lianxi_canshu: {},
    lianxi_id: "",
    lxr_phone: "",
    lianxi_type: "",
    lianxirenLiss: [],
    lianxireTiTle: "",
    lianxirenphone: [],
    phoneLoading: false, // 联系人标讯人数弹框
    lxinrenLOgin: false, //加载
    // 订阅成功
    msg: "订阅成功",

    // 搜索 和 导出搜索 显示高级导出 的判断
    routerPush: "search",
    routerdetails: "",
    // 二进制
    binary: "",

    // 导出搜索
    // gys
    daochusupTotal: "",
    // cgs
    daochuCgstotals: "",
    leave: {
      leave: [],
    },
    market: {
      market: [],
    },
    quarter: {
      quarter: [],
    },
    T_tender: {},
    T_xijie: {},
    T_provider: {},
    T_buyer: {},
    // 导出联系人
    T_contacts: {},
    T_lxrLiss: [],
    daochuloading: false,
    // pk
    T_ds: {},
    T_dsLiss: [],
  }),

  actions: {

    // 重置查询参数
    resetParams() {
      this.title = {};
      this.count = {};
      this.page = {
        currentPage: 1,
        pageSize: 10,
      }
    },

    // 全部标讯
    getsearchs() {
      console.log('---333---')
      this.dataLiss = [];
      this.loading = true;
      let list = {
        ...this.title,
        ...this.count,
        ...this.page,
        ...this.arrange,
      };
      getsearch(list).then((res) => {
        let {
          data: { data },
        } = res;
        this.totals = data.count;
        this.dataLiss = data.list;
        if (data.count > 3000) {
          this.total = 3000;
        } else {
          this.total = data.count;
        }

        this.loading = false;
      });
    },
    // 供应商
    getsuppliers(type) {

      this.suppliersLiss = [];
      this.gysloading = true;
      let list = type ? {
        ...this.title,
        ...this.suppliers,
        ...this.page,
        ...this.summary,
        tab: 1
      } : {
        ...this.title,
        ...this.suppliers,
        ...this.page,
        ...this.summary,
      };
      getsupplier(list).then((res) => {
        let {
          data: { data },
        } = res;
        this.totals = data.count;
        if (!localStorage.getItem("token") || token.level_id < 2) {
          this.suppliersLiss = data.list.slice(0, 6);
        } else {
          this.suppliersLiss = data.list;
        }
        if (data.count > 3000) {
          this.total = 3000;
        } else {
          this.total = data.count;
        }
        this.gysloading = false;
        // console.log(data);
      });
    },
    // 采购商
    getbuyers(type) {
      this.buyerLiss = {};
      this.loading = true;
      let list = type ? { ...this.title, ...this.buyer, ...this.page, tab: 1 } : { ...this.title, ...this.buyer, ...this.page };
      getbuyer(list).then((res) => {
        let {
          data: { data },
        } = res;
        this.totals = data.count;
        if (!localStorage.getItem("token") || token.level_id < 2) {
          this.buyerLiss = data.list.slice(0, 6);
        } else {
          this.buyerLiss = data.list;
        }
        if (data.count > 3000) {
          this.total = 3000;
        } else {
          this.total = data.count;
        }
        this.loading = false;
        // console.log(data);
      });
    },

    // 招标预测
    getforecasts(type) {
      this.forecastsLiss = {};
      this.loading = true;
      let list = type ? {
        name: this.title.title != "" ? this.title.title : "阀门",
        ...this.forecasts,
        ...this.page,
        tab: 1
      } : {
        name: this.title.title != "" ? this.title.title : "阀门",
        ...this.forecasts,
        ...this.page,
      };
      getforecast(list).then((res) => {
        let {
          data: { data },
        } = res;
        this.totals = data.count;
        if (!localStorage.getItem("token") || token.level_id < 2) {
          this.forecastsLiss = data.list.slice(0, 6);
        } else {
          this.forecastsLiss = data.list;
        }
        if (data.count > 3000) {
          this.total = 3000;
        } else {
          this.total = data.count;
        }
        this.loading = false;
        // console.log(data);
      });
    },
    // 招标代理机构
    getenderings() {
      this.agencyLiss = {};
      this.loading = true;
      let list = { ...this.title, ...this.agency, ...this.page };
      getendering(list).then((res) => {
        let {
          data: { data },
        } = res;
        this.totals = data.count;
        this.agencyLiss = data.list;
        if (data.count > 3000) {
          this.total = 3000;
        } else {
          this.total = data.count;
        }
        this.loading = false;
        // console.log(data);
      });
    },
    // 拟建项目
    getprojects(type) {
      this.proposedLiss = {};
      this.loading = true;
      let list = type ? { ...this.title, ...this.proposed, ...this.page, tab: 1 } : { ...this.title, ...this.proposed, ...this.page };
      getproject(list).then((res) => {
        let {
          data: { data },
        } = res;
        this.totals = data.count;
        if (!localStorage.getItem("token") || token.level_id < 2) {
          this.proposedLiss = data.list.slice(0, 6);
        } else {
          this.proposedLiss = data.list;
        }
        if (data.count > 3000) {
          this.total = 3000;
        } else {
          this.total = data.count;
        }
        this.loading = false;
        // console.log(data);
      });
    },
    // 品牌
    getbrands(type) {
      this.brandsLiss = {};
      this.loading = true;
      let pinaiID = {
        pinaiID: this.pinaiID,
        xmdw: this.xmdw,
      };
      let list = type ? {
        ...this.title,
        ...this.page,
        ...this.arrange,
        ...pinaiID,
        ...this.brands,
        tab: 1
      } : {
        ...this.title,
        ...this.page,
        ...this.arrange,
        ...pinaiID,
        ...this.brands,
      };
      getbrand(list).then((res) => {
        let {
          data: { data },
        } = res;
        this.totals = data.count;
        if (!localStorage.getItem("token") || token.level_id < 2) {
          this.brandsLiss = data.list.slice(0, 6);
        } else {
          this.brandsLiss = data.list;
        }
        if (data.count > 3000) {
          this.total = 3000;
        } else {
          this.total = data.count;
        }
        this.loading = false;
        // console.log(data);
      });
    },
    // 公司
    getcompanys() {
      this.corporLiss = {};
      this.loading = true;
      let list = { ...this.title, ...this.corporation, ...this.page };
      getcompany(list).then((res) => {
        let {
          data: { data },
        } = res;
        this.totals = data.count;
        this.corporLiss = data.list;
        if (data.count > 3000) {
          this.total = 3000;
        } else {
          this.total = data.count;
        }
        this.loading = false;
        // console.log(data);
      });
    },
    // 详情
    getcompany_names() {
      let list = {
        company_name: this.company_name,
      };
      getcompany_name(list).then((res) => {
        // console.log(res.data.data);

        if (!res.data.data.length) {
          this.xiangqing = {
            staffNum: "1",
            regCapital: "100",
            representative: "张",
          };
        } else {
          this.xiangqing = res.data?.data[0];
        }
      });
    },
    // 订阅
    subscriptions() {

      const tokens = localStorage.getItem("token")
      if (tokens) {

        let params = {
          type_id: this.id,
          clientid: token.clientid,
          s_condition: this.count,
          kewords: this.title.title,
          token: tokens,
        };
        getbxsubscription(params).then((res) => {
          this.msg = res.data.msg;
          if (res.data.code == 1) {
            ElMessage({
              message: res.data.msg,
              type: "success",
            });
          } else {
            ElMessage({
              message: res.data.msg,
              type: "error",
            });
          }
        });
      } else {
        store.uselogin = true
      }

    },
    // 详情订阅
    subscriptionxq(params) {
      getbxsubscription(params).then((res) => {
        this.msg = res.data.msg;
      });
    },
    getcompany_details() {
      this.zhaobiaoDatalog = true;
      let list = {
        company_name: this.company_name,
        level_id: this.level_id,
      };
      getcompany_detail({
        ...list,
        ...this.solicitation,
        ...this.xiangqing_pages,
      }).then((res) => {
        let {
          data: { data },
        } = res;

        this.zhaobiaoData = data.list;
        this.zhaobaioTiTle = data.count;
        if (data.count > 300) {
          this.total = 300;
        } else {
          this.total = data.count;
        }
        // console.log(data)
        this.zhaobiaoDatalog = false;
      });
    },

    async getNewData(name, page) {
      const res = await tj_dl({ company_name: name })
      this.zhaobiao_digital = res.data.data.dl
      const res_1 = await detail_dl({ company_name: name, currentPage: page, ...this.solicitation })
      // console.log(444,res_1);
      this.zhaobiaoData = res_1.data.data.list
      this.zhaobaioTiTle = res_1.data.data.count
      const res_2 = await lxr_dl({ company_name: name })
      console.log(43342, res_2);
      this.lianxiName = res_2.data.data.list
    },

    // 招
    getcompany_sums() {
      this.xqshujuloading = true;
      let list = {
        company_name: this.company_name,
      };
      getcompany_sum({ ...list }).then((res) => {
        let {
          data: { data },
        } = res;
        this.zhaobiao_digital = data.zhaobiao;
        this.zhongbiao_digital = data.zhongbiao;
        this.xqshujuloading = false;
        // console.log(data)
      });
    },
    // 中
    getcompany_detail_zbs() {
      this.xqshujuloading = true;
      let list = {
        company_name: this.company_name,
        level_id: this.level_id,
      };
      getcompany_detail_zb({
        ...list,
        ...this.winatender,
        ...this.xiangqing_pages,
      }).then((res) => {
        let {
          data: { data },
        } = res;
        // console.log(data);
        if (data.count > 300) {
          this.total = 300;
        } else {
          this.total = data.count;
        }
        this.zhongbaioTiTle = data.count;
        this.zhongbiao_liss = data.list;
        this.xqshujuloading = false;
      });
    },
    // 招标 中标 联系人
    getcompany_detail_lxs() {
      let list = {
        company_name: this.company_name,
      };
      getcompany_detail_lx(list).then((res) => {
        let {
          data: { data },
        } = res;
        this.zhaoblianxiren = data.count;
        this.lianxiName = data.list;
        console.log(data);
      });
    },

    getcompany_detail_zb_lxs() {
      let list = {
        company_name: this.company_name,
      };
      getcompany_detail_zb_lx(list).then((res) => {
        let {
          data: { data },
        } = res;
        console.log(data);
        this.zhongbiaolianxiren = data.count;
        this.zhongbiao_Name = data.list;
      });
    },

    //未中标
    getcompany_detail_wzbs() {
      let list = {
        company_name: this.company_name,
        level_id: this.level_id,
      };
      getcompany_detail_wzb({
        ...list,
        ...this.weizhongbaioTj,
        ...this.xiangqing_pages,
      }).then((res) => {
        let {
          data: { data },
        } = res;
        if (data.count > 300) {
          this.total = 300;
        } else {
          this.total = data.count;
        }
        this.weizhongbiaoLiss = data.list;
        this.weizhongbaioTiTle = data.count;
        // console.log(data);
      });
    },
    // 联系人 getcompany_contacts
    getcompany_contact() {
      this.lxinrenLOgin = true;
      let list = {
        company_name: this.company_name,
        company_type: this.lianxi_id == undefined ? "2" : this.lianxi_id,
      };
      // ...this.weizhongbaioTj,
      // ...this.xiangqing_pages,
      getcompany_contacts({
        ...list,
        ...this.lianxi_canshu,
      }).then((res) => {
        let {
          data: { data },
        } = res;
        console.log(data);
        this.lianxirenLiss = data.list;
        if (data.count > 300) {
          this.lianxireTiTle = 300;
        } else {
          this.lianxireTiTle = data.count;
        }
        this.lxinrenLOgin = false;
      });
    },
    getcompany_contacts_details() {
      this.phoneLoading = true;
      let list = {
        company_name: this.company_name,
        company_type: this.lianxi_id == undefined ? "2" : this.lianxi_id,
        lianxi_type: this.lianxi_type,
        phone: this.lxr_phone,
        ...this.xiangqing_pages,
      };
      // ...this.weizhongbaioTj,
      getcompany_contacts_detail({
        ...list,
      }).then((res) => {
        let {
          data: { data },
        } = res;
        if (data.count > 300) {
          this.total = 300;
        } else {
          this.total = data.count;
        }
        this.lianxirenphone = data.list;
        this.phoneLoading = false;
      });
    },
    // 导出
    // Dataexportbx_bx() {
    //   Dataexportbx().then((res) => {
    //     // console.log(res.data)
    //     this.binary = res.data
    //   });
    // },

    // zb
    T_getsearchs(lust) {
      getsearch(lust).then((res) => {
        let {
          data: { data },
        } = res;
        this.biaoxuntotals = data.count;
        this.dataLiss = data.list;
        if (data.count > 300) {
          this.total = 300;
        } else {
          this.total = data.count;
        }
        console.log(data);
      });
    },
    // gys
    T_getsuppliers(lust) {
      this.gysloading = true;
      getsupplier(lust).then((res) => {
        let {
          data: { data },
        } = res;
        this.daochusupTotal = data.count;
        this.suppliersLiss = data.list;
        if (data.count > 300) {
          this.total = 300;
        } else {
          this.total = data.count;
        }
        this.gysloading = false;
      });
    },
    // cgs
    T_getbuyers(lust) {
      this.buyerLiss = {};
      this.loading = true;
      getbuyer(lust).then((res) => {
        let {
          data: { data },
        } = res;
        this.daochuCgstotals = data.count;
        this.buyerLiss = data.list;
        if (data.count > 300) {
          this.total = 300;
        } else {
          this.total = data.count;
        }
        this.loading = false;
        // console.log(data);
      });
    },
    //导出联系人
    getcontact(list) {
      this.loading = true;
      // let list = {
      //   ...this.T_contacts,
      //   ...this.leave,
      //   ...this.market,
      //   ...this.quarter,
      // };
      getcontacts(list).then((res) => {
        let {
          data: { data },
        } = res;
        this.T_lxrLiss = data;
        this.loading = false;
        // console.log(this.T_lxrLiss);
      });
    },
    //  导出 详情里的竞争对手
    getcompetitors() {
      this.daochuloading = true;
      let list = {
        ...this.corporation,
        ...this.title,
        ...this.xiangqing_pages,
        company_name: this.company_name,
      };
      getcompetitor(list).then((res) => {
        let {
          data: { data },
        } = res;
        if (data.count > 300) {
          this.total = 300;
        } else {
          this.total = data.count;
        }
        this.T_dsLiss = data.list;
        this.daochuloading = false;
        console.log(data);
      });
    },

    shujugetcompetitors(liut) {
      console.log(liut);
      this.daochuloading = true;
      let list = {
        // ...this.T_ds,
        // ...this.corporation,
        // ...this.title,
        // ...this.xiangqing_pages,
        company_name: liut.input1,
      };
      getcompetitor(list).then((res) => {
        let {
          data: { data },
        } = res;
        if (data.count > 300) {
          this.total = 300;
        } else {
          this.total = data.count;
        }
        this.T_dsLiss = data.list;
        this.daochuloading = false;
        console.log(data);
      });
    },
  },
});
